/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cong.javadanang.controller.bean;

import cong.javadanang.model.dao.RoleDAO;
import cong.javadanang.model.dao.UserDAO;
import cong.javadanang.model.dao.impl.RoleDAOImpl;
import cong.javadanang.model.dao.impl.UserDAOImpl;
import cong.javadanang.model.entity.Role;
import cong.javadanang.model.entity.User;
import cong.javadanang.utils.Support;
import java.util.Date;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

/**
 *
 * @author Pham
 */
@ManagedBean
@RequestScoped
public class UserManagement {

    private final UserDAO USER_SERVICE = UserDAOImpl.getInstance();
    private final RoleDAO ROLE_SERVICE = RoleDAOImpl.getInstance();

    private User user;

    public UserManagement() {
    }
    
    
    
    public String createUser(ActionEvent event) {
        String page = "/pages/register.jsf";
        String msg;

        String userName = user.getUserName();
        String pwd = Support.encryptMD5(user.getPwd());
        String fullName = user.getFullName();
        Date dayOfBirth = user.getDayOfBirth();
        boolean gender = user.isGender();
        String email = user.getEmail();
        String phone = user.getPhoneNumber();

        String imagePath;
        if (user.isGender()) {
            imagePath = "images/avatar/avatar_male.jpg";
        } else {
            imagePath = "images/avarar/avatar_female.jpg";
        }
        Role r = ROLE_SERVICE.getRoleById(3);
        String idActive = Support.encryptMD5(userName + new Date().toString());
        User userCreation = new User(r, userName, pwd, fullName, dayOfBirth, gender, email, null, null, phone, null, imagePath, idActive, false, null, null);
        if (!USER_SERVICE.checkUserName(userName)) {
            if (!USER_SERVICE.checkEmail(email)) {
                if (USER_SERVICE.createUser(userCreation)) {
                    Support.sendMail(email, idActive);
                    msg = "Bạn đã tạo account thành công!";
                    page = "/pages/main.jsf?faces-redirect=true";
                } else {
                    msg = "Account create failed!";
                }
            } else {
                msg = "Địa chỉ Email đã tồn tại! Vui lòng chọn lại email khác!";
            }
        } else {
            msg = "Tên đăng nhập đã có người sử dụng, vui lòng chọn lại! ";
        }
        this.addMessages(msg);
        return page;
    }

    public void addMessages(String msg) {
        FacesMessage fm = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, "");
        FacesContext.getCurrentInstance()
                .addMessage(null, fm);
    }

    public User getUser() {
        if (user == null) {
            user = new User();
        }
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
    
    public Date getMaxDate(){
        return new Date();
    }

}
